package net.cmbt.okframe.oknet;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * 配置信息存储�?
 *
 * @author Colin.Yuan
 *         2013-3-15
 */
public class OkPreferences {
    private static final String PREF_CERT_CODES = "dfsfds";
    private static SharedPreferences prefs;

    /**
     * 是否第一次使用
     */
    public static final String ISFRIST = "isfrist";


    public static final String setUserMessPremissCancle = "setUserMessPremissCancle";


    /**
     * 登录Token
     */
    public static final String PREF_ACCESS_TOKEN = "access_token";

    public static final String PREF_ACCESS_IP = "access_tokensfsdfs";
    /**
     * 登录账号
     */
    public static final String PREF_ACCOUNT_NAME = "account_name";
    /**
     * 位置-省份
     */
    public static final String PREF_lOCATION_PROVINCE = "location_province";

    public static final String MOBILENO_PROCE = "mobileno_proce";
    /**
     * 位置-城市
     */
    public static final String PREF_LOCATION_CITY = "location_city";
    /**
     * 位置-lat
     */
    public static final String PREF_LOCATION_LAT = "location_lat";
    /**
     * 位置-long
     */
    public static final String PREF_LOCATION_LONG = "location_long";

    public static final String PREF_ENV_VALUE = "env";

    public static final String PREF_FIRST_RUN = "run";

    public static final String PREF_LAST_UPDATE_TIME = "last_update_time";

    public static final String PREF_LAST_VERSION_DIALOG = "last_version_dialog";

    public static final String PREF_MASK_SHOW = "mask_show";

    public static final String PREF_NEW_CONTENT_UPDATED_AT = "newContentUpdatedAt";

    public static final String PREF_MEMBERINFO = "memberinfo";

    public static final String PREF_QQ_LOGIN_JSON = "qq_login_json";

    public static final String PREF_CERT_CODE = "certification_code";

    public static final String PREF_CERT_CODE1 = "certification_code1";

    public static final String PREF_UPDATE_PARAM1 = "loginInfo";

    public static final String PREF_CERT_COUNT = "cert_count";

    public static final String PREF_SPLASH_IMAGE = "splash_img";

    public static final String INDEX_INFO = "index_info";

    public static final String DEFAULTADDRESS = "DefaultAddress";

    public static final String USERINFO = "userinfo";

    public static final String SENDGOODSBEAN_A = "reverGoodsBean";

    public static final String SENDGOODSBEAN = "sendGoodsBean";


    public static void init(Application app) {
        prefs = app.getSharedPreferences("my_pref", Context.MODE_PRIVATE);
    }


    /**
     * 是否第一次使用
     *
     * @param isFrist
     */
    public static void setFristRun(Boolean isFrist) {
        put(ISFRIST, isFrist);
    }


    public static Boolean isFristRun() {
        return prefs.getBoolean(ISFRIST, true);
    }


    /**
     * 是否第一次使用
     *
     * @param isFrist
     */
    public static void setUserMessPremissCancle(Boolean isFrist) {
        put(setUserMessPremissCancle, isFrist);
    }


    public static Boolean getUserMessPremissCancle() {
        return prefs.getBoolean(setUserMessPremissCancle, false);
    }


    /**
     * 保存手机唯一标识码
     */
    public static void setMobileNO(String name) {
        put(MOBILENO_PROCE, name);
    }

    /**
     * 获取手机唯一标识码
     */
    public static String getMobileNO() {
        return prefs.getString(MOBILENO_PROCE, "");
    }

    /**
     * 保存位置-省份
     */
    public static void setLocationProvince(String name) {
        put(PREF_lOCATION_PROVINCE, name);
    }

    /**
     * 获取位置-省份
     */
    public static String getLocationProvince() {
        return prefs.getString(PREF_lOCATION_PROVINCE, "");
    }

    /**
     * 保存位置-城市
     */
    public static void setLocationCity(String name) {
        put(PREF_LOCATION_CITY, name);
    }

    /**
     * 获取位置-城市
     */
    public static String getLocationCity() {
        return prefs.getString(PREF_LOCATION_CITY, "");
    }

    /**
     * 保存位置-lat
     */
    public static void setLocationLat(String name) {
        put(PREF_LOCATION_LAT, name);
    }

    /**
     * 获取位置-lat
     */
    public static String getLocationLat() {
        return prefs.getString(PREF_LOCATION_LAT, "");
    }

    /**
     * 保存位置-long
     */
    public static void setLocationLong(String name) {
        put(PREF_LOCATION_LONG, name);
    }

    /**
     * 获取位置-long
     */
    public static String getLocationLong() {
        return prefs.getString(PREF_LOCATION_LONG, "");
    }

    /**
     * 保存登录账号
     */
    public static void setAccountName(String name) {
        put(PREF_ACCOUNT_NAME, name);
    }

    /**
     * 获取登录账号
     */
    public static String getAccountName() {
        return prefs.getString(PREF_ACCOUNT_NAME, "");
    }

    /**
     * 保存登录Token
     *
     * @param token
     */
    public static void setAccessToken(String token) {
        put(PREF_ACCESS_TOKEN, token);
    }

    /**
     * 获取登录Token
     */
    public static String getAccessToken() {
        return prefs.getString(PREF_ACCESS_TOKEN, "");
    }

    /**
     * 保存ip地址 做切换用
     *
     * @param token
     */
    public static void setUUID(String token) {
        put(PREF_ACCESS_IP, token);
    }

    /**
     * 获取ip地址 做切换用
     */
    public static String getUUID() {
        return prefs.getString(PREF_ACCESS_IP, "");
    }


    /**
     * 保存ip地址 做切换用
     *
     * @param token
     */
    public static void setDisCount(int token) {
        put(PREF_ACCESS_IP, token);
    }

    /**
     * 获取ip地址 做切换用
     */
    public static int getDisCount() {
        return prefs.getInt(PREF_ACCESS_IP, 0);
    }


    /**
     * 保存Cert_Token
     *
     * @param token
     */
    public static void setCertCode(String token) {
        put(PREF_CERT_CODE, token);
    }

    /**
     * 获取Cert_Token
     */
    public static String getCertCode() {
        return prefs.getString(PREF_CERT_CODE, "");
    }


    /**
     * 保存头像地址
     *
     * @param token
     */
    public static void setPhotoUrl(String token) {
        put(PREF_CERT_CODES, token);
    }

    /**
     * 获取头像地址
     */
    public static String getPhotoUrl() {
        return prefs.getString(PREF_CERT_CODES, "");
    }


    public static void setEnv(String env) {
        put(PREF_ENV_VALUE, env);
    }

    public static String getEnv() {
        return prefs.getString(PREF_ENV_VALUE, "PROD");
    }

    public static void setLastUpdateTime(long t) {
        put(PREF_LAST_UPDATE_TIME, t);
    }

    public static long getLastUpdateTime() {
        return prefs.getLong(PREF_LAST_UPDATE_TIME, 0);
    }

    public static void setLastVersionDialog(long t) {
        put(PREF_LAST_VERSION_DIALOG, t);
    }

    public static long getLastVersionDialog() {
        return prefs.getLong(PREF_LAST_VERSION_DIALOG, 0);
    }

    public static void setMaskPageStatus(long v) {
        put(PREF_MASK_SHOW, v);
    }

    public static long getMaskPageStatus() {
        return prefs.getLong(PREF_MASK_SHOW, 0);
    }

    public static void setNewContentUpdatedAt(String time) {
        put(PREF_NEW_CONTENT_UPDATED_AT, time);
    }

    public static String getNewContentUpdatedAt() {
        return prefs.getString(PREF_NEW_CONTENT_UPDATED_AT, "0");
    }

    public static String getQqLoginJson() {
        return prefs.getString(PREF_QQ_LOGIN_JSON, "");
    }

    public static void setQqLoginJson(String qqjson) {
        put(PREF_QQ_LOGIN_JSON, qqjson);
    }

    public static void setSplashImage(String image) {
        put(PREF_SPLASH_IMAGE, image);
    }


    public static String getSplashImage() {
        return prefs.getString(PREF_SPLASH_IMAGE, "");
    }

    public static void setCertCount(int count) {
        put(PREF_CERT_COUNT, count);
    }


 /*   *//**
     * 获取登陸信息
     */

    public static void setLoginInfo(LoginInfoBean.ResultBean.DataBean loginInfo) {
        try {
            if (loginInfo != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(loginInfo);
                String memberBase64 = new String(Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT));
                put(PREF_UPDATE_PARAM1, memberBase64);
            } else {
                put(PREF_UPDATE_PARAM1, "");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static LoginInfoBean.ResultBean.DataBean getLoginInfo() {
        String memberBase64 = prefs.getString(PREF_UPDATE_PARAM1, "");
        LoginInfoBean.ResultBean.DataBean app = null;
        try {
            if (!memberBase64.equals("")) {
                byte[] base64Bytes = Base64.decode(memberBase64, Base64.DEFAULT);
                ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                app = (LoginInfoBean.ResultBean.DataBean) ois.readObject();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return app;
    }



    /**
     * 保存数据
     *
     * @param name  �?
     * @param value �?
     */
    private static void put(String name, Object value) {
        SharedPreferences.Editor editor = prefs.edit();
        if (value.getClass() == Boolean.class) {
            editor.putBoolean(name, (Boolean) value);
        }
        if (value.getClass() == String.class) {
            editor.putString(name, (String) value);
        }
        if (value.getClass() == Integer.class) {
            editor.putInt(name, ((Integer) value).intValue());
        }
        if (value.getClass() == Float.class) {
            editor.putFloat(name, ((Float) value).intValue());
        }
        if (value.getClass() == Long.class) {
            editor.putLong(name, ((Long) value).longValue());
        }

        editor.commit();

    }

}
